package com.pinda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pinda.commonutils.Page;
import com.pinda.mapper.PdDriverJobMapper;
import com.pinda.mapper.PdTaskTransportMapper;
import com.pinda.service.IPdTaskTransportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pinda.work.entity.PdDriverJob;
import com.pinda.work.entity.PdTaskTransport;
import com.pinda.work.vo.ExeceptiontaskView;
import com.pinda.work.vo.InstitutionaldataKanbanboardView;
import com.pinda.work.vo.pdtasktransportQueryView;
import io.netty.util.internal.MathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 运输任务表 服务实现类
 * </p>
 *
 * @author 黄杰
 * @since 2023-06-13
 */
@Service
public class PdTaskTransportServiceImpl extends ServiceImpl<PdTaskTransportMapper, PdTaskTransport> implements IPdTaskTransportService {


    @Autowired
    private PdTaskTransportMapper taskTransportMapper;

    @Autowired
    private PdDriverJobMapper pdDriverJobMapper;


    /**
     * -- 运输任务管理
     * 查询的表：
     * pd_task_transport 运输任务表
     * pd_truck 车辆表
     * pd_truck_driver 司机表
     * pd_core_org 机构表
     * pd_auth_user
     * pd_transport_trips 车次表
     * pd_transport_trips_truck_driver 车次和司机关系表
     *
     * 根据运输任务表的车辆编号查询到车辆表的车牌号码（车牌号码）
     * 根据运输任务表编号查询车次和司机关系表
     * 根据车次和司机关系表的司机编号查询到司机表的用户编号，
     * 然后根据司机表的用户编号连接查询到用户表的用户姓名。(司机姓名)
     * 根据运输任务的起始机构编号连接查询机构表的机构姓名(起始机构和调度机构)
     * 根据运输任务的目的机构编号连接查询到机构表的机构名称(目的机构)
     * @param page
     * @return
     */
    @Override
    public PageInfo<pdtasktransportQueryView> querypageInfoalltasktansportService(Page<pdtasktransportQueryView> page) {
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        return new PageInfo<pdtasktransportQueryView>(this.taskTransportMapper.querytaskview(page.getSearch()));
    }

    /**
     * 机构数据看板业务编写
     * @return
     */
    @Override
    public InstitutionaldataKanbanboardView querynstitutionaldataKanbanboardService() {
        InstitutionaldataKanbanboardView institutionaldataKanbanboardView = new InstitutionaldataKanbanboardView();
        //查询司机总数
        institutionaldataKanbanboardView.setDriverCount( this.taskTransportMapper.querydriverCount());
        //待发车司机
        institutionaldataKanbanboardView.setReadyCount(this.pdDriverJobMapper.queryreadyCount());
        //查询在途司机
        institutionaldataKanbanboardView.setWayCount(this.pdDriverJobMapper.querywayCount());
        //查询待返回司机
        institutionaldataKanbanboardView.setNoreturnCount(this.pdDriverJobMapper.querynoreturnCount());
        //运单总数
        int querywaybillcount = this.taskTransportMapper.querywaybillcount();
        institutionaldataKanbanboardView.setWaybillCount(querywaybillcount);
        // 空载车百分比
        int querynull = this.taskTransportMapper.querynull();
        double i = ((double) querynull/(double) querywaybillcount)*100;
        System.out.println("空载车百分比>>>>>>>>>>>"+i);
        institutionaldataKanbanboardView.setNullpercentage((int)i);
        //半载百分比
        int queryhalf = this.taskTransportMapper.queryhalf();
        double i1 = ((double) queryhalf/(double) querywaybillcount)*100;
        System.out.println("半载百分比>>>>>>>>>>>"+i1);
        institutionaldataKanbanboardView.setHalfpercentage((int)i1);
        //满载百分比
        int queryfull = this.taskTransportMapper.queryfull();
        double i2 = ((double) queryfull/(double) querywaybillcount)*100;
        System.out.println("满载百分比>>>>>>>>>>>"+i2);
        institutionaldataKanbanboardView.setFullpercentage((int)i2);
        //待发车
        institutionaldataKanbanboardView.setReadygotoCount(this.taskTransportMapper.querytaskcount(1));
        //运输中
        institutionaldataKanbanboardView.setIntransitCount(this.taskTransportMapper.querytaskcount(2));
        //已到达
        institutionaldataKanbanboardView.setArrivedCount(this.taskTransportMapper.querytaskcount(4));
        //发出准时率
        int querystartontime = this.taskTransportMapper.querystartontime();
        double i3 = ((double) querystartontime/(double) querywaybillcount)*100;
        System.out.println("发出准时率>>>>>>>>>>>>>>"+i3);
        institutionaldataKanbanboardView.setStartontime((int)i3);
        //到达准时率
        int queryendontime = this.taskTransportMapper.queryendontime();
        double i4 = ((double) queryendontime/(double) querywaybillcount)*100;
        System.out.println("到达准时率>>>>>>>>>>>>>>"+i4);
        institutionaldataKanbanboardView.setEndontime((int)i4);
        //异常运输任务总数
        int queryexceptioncount = this.taskTransportMapper.queryexceptioncount();
        institutionaldataKanbanboardView.setExceptionCount(queryexceptioncount);
        //异常占比
        double i5 = ((double) queryexceptioncount/(double) querywaybillcount)*100;
        System.out.println("异常占比>>>>>>>>>>>>>>"+i5);
        institutionaldataKanbanboardView.setExceptionpercentage((int)i5);
        //人均运行时长
        String queryavgdriverjob = this.pdDriverJobMapper.queryavgdriverjob();
        institutionaldataKanbanboardView.setAlltransittime(queryavgdriverjob);
        return institutionaldataKanbanboardView;
    }

    @Override
    public List<ExeceptiontaskView> queryExceptionTaskService() {
        PageHelper.startPage(1,4);
        return this.taskTransportMapper.queryexceptiontask();
    }

    @Override
    public List<ExeceptiontaskView> queryRealtimeService() {
        PageHelper.startPage(1,7);
        return this.taskTransportMapper.queryrealtime();
    }

}
